package com.me.mygdxgame.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.me.mygdxgame.util.Constants;
import com.me.mygdxgame.util.GamePreferences;

public class MenuScreen extends AbstractGameScreen {

	private Stage stage;
	private TextureAtlas menuScreen;
	private Image imgBackground;
	private Image btnMenuPlay;
	private Image btnMenuSound;
	private Table layerBackground;
	private Table layerControls;
	
	private Skin skinMenu;
	
	public MenuScreen(Game game) {
		super(game);
		
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0x64 / 255.0f,
				0x95 / 255.0f,
				0xed / 255.0f,
				0xff / 255.0f);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(delta);
		stage.draw();
//		Table.drawDebug(stage);
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(800, 480, false);
	}

	@Override
	public void show() {
		stage = new Stage();
		Gdx.input.setInputProcessor(stage);
		rebuildStage();
	}
	
	private void rebuildStage(){
		menuScreen = new TextureAtlas(Gdx.files.internal(Constants.TEXTURE_ATLAS_UI));
		skinMenu = new Skin(menuScreen);
		
		//build all layers
		layerBackground = buildBackgroundLayer();
		layerControls = buildControlsLayer();
		
		//assemble stage for menu screen
		stage.clear();
		Stack stack = new Stack();
		stage.addActor(stack);
		stack.setSize(800, 480);
		stack.add(layerBackground);
		stack.add(layerControls);
	}

	private Table buildControlsLayer() {
		Table layer = new Table();
		btnMenuPlay = new Image(menuScreen.findRegion("Button_Play_noGC"));
		btnMenuPlay.addListener(new ActorGestureListener(){

			@Override
			public void touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchDown(event, x, y, pointer, button);
				btnMenuPlay.addAction(Actions.alpha(0.8f));
			}

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				btnMenuPlay.addAction(Actions.alpha(1));
				game.setScreen(new GameScreen(game));
			}
			
		});
		btnMenuSound = new Image(skinMenu, "Button_sound_on");
		btnMenuSound.addListener(new ActorGestureListener(){

			@Override
			public void touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchDown(event, x, y, pointer, button);
				btnMenuSound.addAction(Actions.alpha(0.8f));
			}

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				super.touchUp(event, x, y, pointer, button);
				btnMenuSound.addAction(Actions.alpha(1));
				if(GamePreferences.instance.getSound()){
					btnMenuSound.setDrawable(skinMenu, "Button_sound_off");
					GamePreferences.instance.setSound(false);
				}else{
					btnMenuSound.setDrawable(skinMenu, "Button_sound_on");
					GamePreferences.instance.setSound(true);
				}
				
			}
			
		});
		layer.addActor(btnMenuPlay);
		layer.addActor(btnMenuSound);
		btnMenuSound.setPosition(20, 30);
		btnMenuPlay.setPosition(410, 120);
		return layer;
	}

	private Table buildBackgroundLayer() {
		Table layer = new Table();
		imgBackground = new Image(menuScreen.findRegion("Default"));
		layer.addActor(imgBackground);
		return layer;
	}

	@Override
	public void dispose() {
		stage.dispose();
		skinMenu.dispose();
	}

	@Override
	public void hide() {
		stage.dispose();
		skinMenu.dispose();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	

}
